package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.YjhApplication;
import com.hxkcloud.patient.activitys.ArticleDetailActivity;
import com.hxkcloud.patient.activitys.MonitorInputActivity;
import com.hxkcloud.patient.beans.PatientRemindBean;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;

import java.util.Calendar;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/13.
 */
public class RemindListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<PatientRemindBean> datas;
    private LayoutInflater mInflater;
    private int dateType = 0;//日期早于今天0等于今天1今天后2

    private ResultSelectedListener listener;

    public RemindListAdapter(Context mContext, List<PatientRemindBean> datas,
                             RecyclerView recyclerview, ResultSelectedListener listener) {
        this.mContext = mContext;
        this.datas = datas;
        this.listener = listener;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setData(List<PatientRemindBean> datas, int type) {
        this.datas = datas;
        this.dateType = type;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return datas.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        // TODO Auto-generated method stub


        Holder mHolder = (Holder) holder;

        mHolder.timeTV.setText(datas.get(position).getTime());
        //日期早于今天0等于今天1今天后2
        if (dateType == 0) {
            mHolder.remindLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.stroke_line));
        } else if (dateType == 2) {
            mHolder.timeTV.setTextColor(mContext.getResources().getColor(R.color.light_green));
            mHolder.remindLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_green));
        } else {
            int choosehour = Integer.valueOf(datas.get(position).getTime().split(":")[0]);
            int chooseminute = Integer.valueOf(datas.get(position).getTime().split(":")[1]);
            Calendar cal = Calendar.getInstance();
            int curhour = cal.get(Calendar.HOUR_OF_DAY);
            int curminute = cal.get(Calendar.MINUTE);
            if (choosehour < curhour) {
                mHolder.timeTV.setTextColor(mContext.getResources().getColor(R.color.stroke_line));
                mHolder.remindLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.stroke_line));
            } else if (choosehour > curhour) {
                mHolder.timeTV.setTextColor(mContext.getResources().getColor(R.color.light_green));
                mHolder.remindLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_green));
            } else {
                if (chooseminute < curminute) {
                    mHolder.timeTV.setTextColor(mContext.getResources().getColor(R.color.stroke_line));
                    mHolder.remindLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.stroke_line));
                } else {
                    mHolder.timeTV.setTextColor(mContext.getResources().getColor(R.color.light_green));
                    mHolder.remindLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.light_green));
                }
            }
        }

        //设置布局管理器
        mHolder.recyclerView.removeAllViews();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mHolder.recyclerView.setLayoutManager(linearLayoutManager);
//        ListDividerItemDecoration itemDecoration = new ListDividerItemDecoration(
//                mContext, ListDividerItemDecoration.VERTICAL_LIST);
//        mHolder.recyclerView.addItemDecoration(itemDecoration);
        //设置适配器
//        List<NoticeFunctionBean> beans = new ArrayList<>();
        RemindApater mAdapter = new RemindApater(mContext, datas.get(position).getFunctionList(), mHolder.recyclerView);
        mHolder.recyclerView.setAdapter(mAdapter);
        mAdapter.setResultListener(new RemindApater.ResultSelectedListener() {
            @Override
            public void onSelected(String type, int p) {
                if (datas.get(position).getFunctionList().get(p).getType().equals("1")) {
                    Intent intent = new Intent(mContext, ArticleDetailActivity.class);
                    intent.putExtra("articleId", datas.get(position).getFunctionList().get(p).getItemId());

                    intent.putExtra("name", datas.get(position).getFunctionList().get(p).getContent());
                    intent.putExtra("acctType", YjhApplication.articlekind_course);
                    mContext.startActivity(intent);
                    YiJiaHuNetWorkUtils.execOrder(datas.get(position).getFunctionList().get(p).getOrderId());
                } else if (datas.get(position).getFunctionList().get(p).getType().equals("0")) {
                    Intent intent = new Intent(mContext, MonitorInputActivity.class);
                    intent.putExtra("orderId", datas.get(position).getFunctionList().get(p).getOrderId());
                    intent.putExtra("monitorId", datas.get(position).getFunctionList().get(p).getItemId());
                    mContext.startActivity(intent);
                }
            }
        });
//        mAdapter.setData(datas.get(position).getFunctionList());


    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_remindlist_layout, null);
        RecyclerView.ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    /*
     * Holder
     */
    public class Holder extends RecyclerView.ViewHolder {

        private TextView timeTV;
        private View remindLayout;
        private RecyclerView recyclerView;

        public Holder(View view) {
            super(view);
            timeTV = (TextView) view.findViewById(R.id.remindlist_item_tv_time);
            remindLayout = view.findViewById(R.id.remindlist_item_layout_remind);
            recyclerView = (RecyclerView) view.findViewById(R.id.remindlist_item_recyclerView);
        }
    }

    public interface ResultSelectedListener {
        void onSelected(String type, String info);
    }


}
